package com.screen.screendisplay.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.dryport.DryportOpermonShip;
import com.screen.screendisplay.mapper.dryport.DryportOpermonShipMapper;
import com.screen.screendisplay.service.dryport.DryportOpermonShipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.dryport.ShipVO;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 来船信息表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportOpermonShipServiceImpl extends ServiceImpl<DryportOpermonShipMapper, DryportOpermonShip> implements DryportOpermonShipService {

    @Override
    public List<ShipVO> getShipInfoStatistics(Integer year) {
        // 设置默认年份为2025
        if (year == null) {
            year = 2025;
        }
        
        // 查询指定年份的数据
        LambdaQueryWrapper<DryportOpermonShip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DryportOpermonShip::getYear, year);
        queryWrapper.orderByAsc(DryportOpermonShip::getId);
        List<DryportOpermonShip> entityList = this.list(queryWrapper);
        
        // 转换为VO对象列表
        List<ShipVO> result = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        // 船只类型映射：0-C70,1-C70E,2-KV80,3-其他
        String[] shipTypes = {"C70", "C70E", "KV80", "其他"};
        
        for (DryportOpermonShip entity : entityList) {
            ShipVO vo = new ShipVO();
            
            // 根据ship_type生成船只编号，格式为：类型-编号
            // 如果shipyard字段已经包含了完整的船只编号，直接使用
            if (entity.getShipyard() != null && entity.getShipyard().contains("-")) {
                vo.setShipNumber(entity.getShipyard());
            } else {
                // 否则根据ship_type和id生成
                String shipType = entity.getShipType() != null && entity.getShipType() < shipTypes.length 
                    ? shipTypes[entity.getShipType()] : "其他";
                vo.setShipNumber(shipType + "-" + String.format("%04d", 1000 + entity.getId()));
            }
            
            vo.setLoadCapacity(entity.getLoadCapacity());
            vo.setShipLong(entity.getShipLong());
            vo.setTotalTonnage(entity.getTotalTonnage());
            vo.setDraft(entity.getDraft());
            vo.setDeparturePort(entity.getDeparturePort());
            
            // 格式化日期为字符串
            if (entity.getEstimatedArrival() != null) {
                vo.setEstimatedArrival(dateFormat.format(entity.getEstimatedArrival()));
            }
            if (entity.getEstimatedDeparture() != null) {
                vo.setEstimatedDeparture(dateFormat.format(entity.getEstimatedDeparture()));
            }
            
            result.add(vo);
        }
        
        return result;
    }

}
